; docformat = 'idl'

;+
; NAME:
;   STRINGER
; PURPOSE:
;       This routine extend the output of cluster_tree coupled with the cluster_member clustering routines 
;
; CALLING SEQUENCE:
;       result = cluster_distribution(input)
;
; INPUTS:
;       data           : original data
;       cluster_matrix : clusterization step matrix, output of cluster_member()
;
; MODIFICATION HISTORY:
;       Written byMario D'Amore, German Aerospace Center (DLR), 2009.
;-
function cluster_distribution,data,cluster_matrix

;    compile_opt idl2

    ON_ERROR, 0

;---------------------------
;------ NEW ROUTINE --------
;---------------------------

n_data_dimension = N_ELEMENTS(data(*,0))
n_meas = N_ELEMENTS(data(0,*))

n_total_objects = 2*n_meas-2-1             ;- less functions call this way
n_objects       = n_meas
cluster_matrix_size=SIZE(cluster_matrix,/DIM)
n_step      = cluster_matrix_size(1)

address=intarr(n_total_objects-n_objects+1+1)
address(*)=-1

step_address=intarr(n_step)
clst_dist={n_cluster_steps:n_step,step_address:step_address}  ;-- root value for structure will contain all cluster
step_address_shift=N_TAGS(clst_dist)
;PRINT,'Step/Singl/N_Clst/Uniq_Clst'
;PRINT,'  -1    '+stringer(n_objects)+'   0    0'

for step=0,n_step-1 do begin ;--- Cycling through the clustering steps
  ;--- cycle-changing tmp variables -----------
; single_object_step = where(single_matrix(*,step) eq 1b,n_single_objects,COMPLEMENT=clst_index_total,NCOMPLEMENT=n_clustered_total)    ;- old model
  single_object_step = where( cluster_matrix(*,step) le n_objects-1,n_single_objects,COMPLEMENT=clst_index_total,NCOMPLEMENT=n_clustered_total) ;- new model, less input to the routine
  
  ;--------------------------------------------
  address(*)=-1
;  help,n_single_objects,n_clustered_total
    
 if n_clustered_total ne 0 then begin  ;---  check if there are more than 0 cluster at this step 
    ;--- sort and finding uniques clusters among the indexed matrix
    clst            = cluster_matrix(clst_index_total,step)
    clst_sort_index = sort(clst)
    clst_sort       = clst(clst_sort_index)
    clst_uniq_index = uniq(clst_sort)
    clst_uniq       = clst_sort(clst_uniq_index)
  
    n_uniq_clst     = N_ELEMENTS(clst_uniq)

step_tmp={Step_name:step,N_uniq_clst:n_uniq_clst,address:address} ;-- root value for step structure, just number of unique cluster
n_step_tmp_tags_ini=N_TAGS(step_tmp) ;- initial shift of address vector

      for in_cl=0,n_uniq_clst-1 do begin  ;--- Cycling through the clusters
            cluster_belonging_index = where(cluster_matrix(*,step) eq clst_uniq(in_cl),n_sp,COMPLEMENT=cluster_NOT_belonging_index)
            data_centroid      = total(data(*,cluster_belonging_index),2)/n_sp
            cl_name=cluster_matrix(cluster_belonging_index(0),step)            
;      color=254*(float(step)/(n_step-1))
;      plots,data_centroid(0),data_centroid(1),col=color,psym=8
;      XYouts,data_centroid(0)+0.1,data_centroid(1),stringer_int(cl_name),col=color
            tmp = {cl_name:cl_name,n_object:n_sp,index:cluster_belonging_index,centroid:data_centroid}
            address[cl_name-n_objects]=in_cl+n_step_tmp_tags_ini
            step_tmp=CREATE_STRUCT(step_tmp,'Cluster_'+stringer(cl_name),tmp)
;            print,clst_uniq(in_cl),n_sp,' - index: ',cluster_belonging_index
      endfor

 (step_tmp.address)=address
 clst_dist=CREATE_STRUCT(clst_dist,'Step_'+stringer(step),step_tmp)
 step_address[step] = step+step_address_shift

  endif else n_uniq_clst=0

;print,'   '+stringer([step,n_single_objects,n_clustered_total,n_uniq_clst])

endfor

clst_dist.step_address=step_address

;---------------------------
;----- END NEW ROUTINE -----
;---------------------------

return,clst_dist

end